﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ViewModels;
using Microsoft.Practices.ServiceLocation;
using Caliburn.Core;
using System.Reflection;

namespace XWord.WPF.ApplicationFramework
{
    public class ViewStrategy : DefaultViewLocator
    {
        public ViewStrategy(IAssemblySource assemblySource, IServiceLocator serviceLocator)
            : base(assemblySource, serviceLocator)
        {
        }

        protected override IEnumerable<string> GetTypeNamesToCheck(Type modelType, string context)
        {
            string className = modelType.Name.Substring(0, modelType.Name.IndexOf("Presenter")) + "View";

            string fullClassName = null;
            if (Assembly.GetEntryAssembly() == modelType.Assembly)
                fullClassName = string.Format("XWord.GUI.Views.{0}", className);
            else
                fullClassName = string.Format("XWord.GUI.Views.{0}.{1}", modelType.Namespace.Split('.')[1], className);

            return base.GetTypeNamesToCheck(modelType, context)
                .Union(new[] { fullClassName });
        }
    }
}
